package org.itboys.mobile.controller.mobile.common;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;

import org.itboys.mobile.service.mongo.mobile.common.SundayMobileCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2017年2月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_验证码_controller。
 */
@RestController
@RequestMapping("/sunday/mobile/code")
@Api(value = "code",description = "验证码")
public class SundayMobileCodeController extends BaseController {
    @Autowired
    private SundayMobileCodeService codeService;



    /**
     * 验证码发送
     * @param request
     * @param response
     * @param mobile
     * @param type
     */
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ApiOperation(value = "发送验证码。code!=0直接提示消息")
    public void send(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "mobile", value = "手机号") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(required = true, name = "type", value = "验证码类型。1登陆，2注册（手机号加验证码登录），3修改密码，4忘记密码，5设置支付密码，6修改支付密码") @RequestParam(value = "type", required = true) Integer type){

        try {
            int resultCode= codeService.sendCode(mobile,type);
            String msg= CommonConstants.SUCCESS;
            Integer code= CommonConstants.SUCCESS_CODE;
            if(resultCode==-2){
                msg="请输入正确的手机号。";
            }
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(
                            code,msg));
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/sunday/mobile/cid/upload--------"+e);
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));

        }
    }
    /**
     * 验证码发送
     * @param request
     * @param response
     * @param mobile
     * @param type
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ApiOperation(value = "检验验证码。code!=0直接提示消息")
    public void check(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "mobile", value = "手机号") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(required = true, name = "type", value = "验证码类型。1注册，2登陆（手机号加验证码登录），3修改密码，4忘记密码，5设置支付密码，6修改支付密码") @RequestParam(value = "type", required = true) Integer type,
            @ApiParam(required = true, name = "code", value = "验证码")@RequestParam(value = "code", required = true) String code){

        try {
            int resultCode= codeService.checkCode(mobile,code,type);
            String result_msg= CommonConstants.SUCCESS;
            Integer result_code= CommonConstants.SUCCESS_CODE;
            if(resultCode==-2){
                result_msg="请输入正确的手机号。";
                result_code=resultCode;
            }else if(resultCode==-3){
                result_msg="验证码错误。";
                result_code=resultCode;
            }
            else if(resultCode==-1){
                //result_msg="验证码错误。";
                result_code=resultCode;
            }
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(
                            result_code,result_msg));
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/sunday/mobile/code/send--------"+e);
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));

        }
    }

}
